package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 地区资源的controller
 */
@Controller
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private IRegionService regionService;

    @RequestMapping("/selectByParentId")
    @ResponseBody
    public Object city(Long parentId) {
        //第一级的地区的集合
        List<Region> list = regionService.selectForList(parentId);
        List<Map> mapList = new ArrayList<Map>();
        for (Region r : list) {
            mapList.add(r.toMap());
        }
        return mapList;
    }

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {

        return "region/list";
    }

    @RequestMapping("/saveOrUpdate")@ResponseBody
    public Object saveOrUpdate(Region region){
        JSONResult json = new JSONResult();
        try {
            regionService.saveOrUpdate(region);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }


}
